package com.wk.springboot.jdk8.time;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.ValueRange;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * @User admin
 * @Date 2020/11/19 13:54
 **/
public class TimeTest {

    private static final String PATTERN = "yyyyMMdd";

    public static void main(String[] args) throws InterruptedException {
//        simpleDateFormatTest();
//        localDateTimetest();
//        instantTest();
//        durationTest();
        periodTest();
    }

    private static void simpleDateFormatTest() throws InterruptedException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN);
        Date date = new Date();
        List<Callable<String>> list = new ArrayList<>();

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10,10,1000,
                TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(5),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 10; i++) {
            list.add(()->
                Thread.currentThread().getName()+simpleDateFormat.format(date)
            );
        }
        List<Future<String>> futures = threadPoolExecutor.invokeAll(list);
        futures.forEach(x-> {
            try {
                System.out.println("x = " + x.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        threadPoolExecutor.shutdownNow();

    }

    private static void localDateTimetest() {
        //创建LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime = " + localDateTime);//localDateTime = 2020-11-20T09:32:14.393
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 9, 20, 9, 22);
        System.out.println("localDateTime1 = " + localDateTime1);//localDateTime1 = 2020-09-20T09:22

        //比较两个时间
        int compare = localDateTime.compareTo(LocalDateTime.now());
        System.out.println("compare = " + compare); //-1 表示小于

        //格式化日期
        String format = localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println("format = " + format); //20201120

        //得到这一周的第几天
        DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
        System.out.println("dayOfWeek.getValue() = " + dayOfWeek.getValue());//5

        //是否比比较的日期晚
        boolean after = localDateTime.isAfter(LocalDateTime.now());
        System.out.println("after = " + after); //false

        //加5天
        LocalDateTime plus = localDateTime.plus(5, ChronoUnit.DAYS);
        System.out.println("plus = " + plus);//plus = 2020-11-25T09:51:37.909

        //减5天
        LocalDateTime minus = localDateTime.minus(5, ChronoUnit.DAYS);
        System.out.println("minus = " + minus);//minus = 2020-11-15T09:57:20.009

        //截取日期
        LocalDate localDate = localDateTime.toLocalDate();
        System.out.println("localDate = " + localDate);//2020-11-20

        //截取到指定单位的日期，其他小的单位都归零
        LocalDateTime year = localDateTime.truncatedTo(ChronoUnit.DAYS);
        System.out.println("year = " + year);//year = 2020-11-20T00:00

        //查询这个日期指定单位下的范围
        ValueRange range = localDateTime.range(ChronoField.DAY_OF_MONTH);
        System.out.println("range = " + range);//1 - 30 这个月有30天

        //日期与指定日期相差指定的单位
        long until = localDateTime1.until(LocalDateTime.now(), ChronoUnit.SECONDS);
        System.out.println("until = " + until); //5273169

        //矫正器，
        LocalDateTime dayOfMonth = localDateTime.withDayOfMonth(5);
        System.out.println("dayOfMonth = " + dayOfMonth);//dayOfMonth = 2020-11-05T10:09:32.761 指定这个月第五天的日期

        //矫正器，指定矫正器来矫正时间
        LocalDateTime with = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("with = " + with);//2020-11-01T10:12:49.476
    }

    private static void instantTest() {
        //创建时间戳
        Instant instant = Instant.now();
        Instant instant1 = Instant.parse("2007-12-03T10:15:30.00Z");
        System.out.println("instant = " + instant);
        System.out.println("instant1 = " + instant1);

        //比较两个时间戳
        int compare = instant.compareTo(instant1);
        System.out.println("compare = " + compare);

        //距1970的毫秒值
        long epochMilli = instant.toEpochMilli();
        System.out.println("epochMilli = " + epochMilli); //1605839156325
    }

    private static void durationTest() {

        //创建
        Duration duration = Duration.parse("P2D");
        Duration duration1 = Duration.between(LocalDateTime.now(),LocalDateTime.of(2020,3,3,3,3));
        Duration duration2 = Duration.of(5, ChronoUnit.DAYS);
        System.out.println("duration = " + duration);
        System.out.println("duration1 = " + duration1);
        System.out.println("duration2 = " + duration2);

        //距1970 的毫秒数
        long millis = duration.toMillis();
        System.out.println("millis = " + millis); //172800000

    }

    private static void periodTest() {

        //创建
        Period period = Period.ofDays(5);
        Period period1 = Period.of(5, 3, 2);
        System.out.println("period = " + period);
        System.out.println("period1 = " + period1);

        //判断是否为零
        boolean zero = period.isZero();
        System.out.println("zero = " + zero);
    }
}
